package com.Dao.Imp;
import com.Dao.UpdateBookInfoDao;
import com.Utils.C3P0Utils;
import com.pojo.FindBook;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.stereotype.Service;
import java.sql.SQLException;
import java.util.List;
@Service
@Slf4j
public class UpdateBookInfoDaoImp implements UpdateBookInfoDao {
    @Override
    public void updateRecommendQuantity(int bookId, int quantity,String username) throws SQLException {
        QueryRunner queryRunner=new QueryRunner(C3P0Utils.getDataSource());
        String sql1="select recommendQuantity from books_detail where id=?";
        String sql="update books_detail set recommendQuantity=? where id=?";
        String sql2="insert into recommend(bookId,username,recommendQuantity)values (?,?,?)";
        List<FindBook>findBooks=queryRunner.query(sql1,new BeanListHandler<>(FindBook.class),bookId);
        System.out.println(findBooks.get(0).getRecommendQuantity());
        queryRunner.update(sql,findBooks.get(0).getRecommendQuantity()+quantity,bookId);
        queryRunner.update(sql2,bookId,username,quantity);
    }
    @Override
    public void updatePraiseQuantity(int praiseNum,int remarkId) throws SQLException {
        QueryRunner queryRunner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="update remark set praise=? where id=?";
        queryRunner.update(sql,praiseNum,remarkId);
    }
}
